package com.usian.user.service.impl


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.usian.common.BaseServiceimpl
import com.usian.common.constans.user.UserConstants.AdminConstans
import com.usian.model.article.pojos.ApAuthor
import com.usian.model.common.dtos.PageResponseResult
import com.usian.model.common.dtos.ResponseResult
import com.usian.model.media.pojos.WmUser
import com.usian.model.user.dtos.AuthDto
import com.usian.model.user.pojos.ApUserRealName
import com.usian.user.feign.ArticleFeign
import com.usian.user.feign.WemediaFeign
import com.usian.user.mapper.ApUserMapper
import com.usian.user.mapper.ApUserRealnameMapper
import com.usian.user.service.ApUserRealNameService
import io.seata.spring.annotation.GlobalTransactional
import org.apache.commons.beanutils.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.*

@Service
open class ApUserRealNameServiceImpl : BaseServiceimpl<ApUserRealnameMapper, ApUserRealName>(), ApUserRealNameService {
    @Autowired
    private lateinit var apUserMapper: ApUserMapper
    @Autowired
    private lateinit var wemediaFeign: WemediaFeign
    @Autowired
    private lateinit var articleFeign: ArticleFeign
    override fun loadListByStatus(dto: AuthDto): ResponseResult<*> {
        if (dto == null) return errorResult
        if (dto.status == null) dto.status = 1.toShort()
        val page = Page<ApUserRealName>(dto.page.toLong(), dto.size.toLong())
        val iPage = page(page, QueryWrapper<ApUserRealName>().apply{eq("status", dto.status)})
        return PageResponseResult(dto.page, dto.size, iPage.total.toInt()).apply { data = iPage.records }
    }
@Transactional
@GlobalTransactional
    override fun updateStatusById(dto: AuthDto, status: Short): ResponseResult<*>? {
        if (dto?.id == null ||status == null || status != AdminConstans.FAIL_AUTH && status != AdminConstans.PASS_AUTH) return errorResult;
        updateById(ApUserRealName().apply {
            id=dto.id
            this.status=status
            reason=dto.msg ?: ""
        })
        if (status== AdminConstans.PASS_AUTH){
            val result: ResponseResult<Any>? = createWmAndAuthor(dto)
            if (result!=null) return result
        }
        return sucResult
    }

    private fun createWmAndAuthor(dto: AuthDto): ResponseResult<Any>? {
        var userRealName = getById(dto.id)//认证对象
        var apUser = apUserMapper.selectById(userRealName.userId)//用户对象
        //创建自媒体人
        var wmUser = wemediaFeign.findByName(apUser.name)
        if (wmUser == null){
            wmUser=WmUser().apply {
                BeanUtils.copyProperties(this,apUser)
                createdTime= Date()
                status= AdminConstans.PASS_AUTH.toInt()
                apUserId=apUser.id
            }
            wemediaFeign.save(wmUser)
        }
        //创建作者
        var author=articleFeign.findByUserId(apUser.id)
        if (author == null){
            articleFeign.save(ApAuthor().apply {
                name=wmUser.name
                type=2
                createdTime= Date()
                userId=apUser.id
            })
        }

        apUser.flag=1//自媒体人
        apUserMapper.updateById(apUser)
        return null
    }


}
